package com.example.saifly.inPagePlan.cureMethod;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.common.Activity;
import com.example.common.Fragment;
import com.example.saifly.R;
import com.example.saifly.inPagePlan.cureMethod.fragment.DailyNursingFragment;
import com.example.saifly.inPagePlan.cureMethod.fragment.EatingHealthFragment;
import com.example.saifly.inPagePlan.cureMethod.fragment.SpecialSeasonFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ShengyiJi
 * @Description:护理知识页面activity
 * @Date:
 */
@SuppressLint("Registered")
public class CureMethodActivity extends Activity {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<Fragment> fragmentList;
    private ImageView image_return;

    private String titles[]={"日常护理","饮食健康","特殊季节"};

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_plan_cure_method;
    }

    @Override
    protected void init() {
        super.init();

        tabLayout=findViewById(R.id.cure_method_tab);
        viewPager=findViewById(R.id.cure_method_viewpager);
        image_return = findViewById(R.id.cure_method_return);

        initViewPager();

        initTab();

        doJump();

    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager(){
        fragmentList=new ArrayList<>();
        fragmentList.add(new DailyNursingFragment());
        fragmentList.add(new EatingHealthFragment());
        fragmentList.add(new SpecialSeasonFragment());

        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager(),this,fragmentList);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
    }

    /**
     * 初始化按键文字
     */
    private void initTab(){
        for(int i=0;i<3;i++){
            tabLayout.getTabAt(i).setText(titles[i]);
        }
    }

    /**
     * 跳转逻辑
     */
    private void doJump()
    {
        image_return.setOnClickListener(view -> finish());
    }

    /**
     * 设置适配器
     */
    class ViewPagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> listFragment;
        Context context;

        public ViewPagerAdapter(@NonNull FragmentManager fm, Context context, List<Fragment> listFragment) {
            super(fm);
            this.context=context;
            this.listFragment=listFragment;
        }

        @NonNull
        @Override
        public androidx.fragment.app.Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public int getCount() {
            return listFragment.size();
        }
    }
}
